#!/bin/bash

echo "🔨 重新编译 cbp2make_ai 项目"
echo "================================"

# 清理并重新构建
rm -rf bin .objs
mkdir -p bin .objs

# 编译设置 - 使用 NO_CURL 避免网络依赖
INCLUDES="-I../cbp2make/lib -I../cbp2make/lib/tinyxml -I../cbp2make/src -Ilib -Isrc"
CXXFLAGS="-std=c++11 -DAICODER_ENHANCED -m64 -DNO_CURL"

echo "📝 编译所有必要文件..."

# 编译函数
compile_file() {
    local file=$1
    local base=$(basename "$file" .cpp)
    echo "  编译: $base.cpp"
    g++ $INCLUDES $CXXFLAGS -c "$file" -o ".objs/$base.o" >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "    ✅ 成功"
        return 0
    else
        echo "    ❌ 失败"
        # 显示前3行错误信息
        g++ $INCLUDES $CXXFLAGS -c "$file" -o ".objs/$base.o" 2>&1 | head -5
        return 1
    fi
}

echo "🔸 AI 模型核心文件:"
compile_file "src/ai_model_config.cpp"
compile_file "src/unified_ai_client.cpp"
compile_file "src/aicoder_enhancements.cpp"

echo ""
echo "🔸 cbp2make 核心文件:"
CBP_FILES=(
    "../cbp2make/src/cbp2make.cpp"
    "../cbp2make/src/cbproject.cpp"
    "../cbp2make/src/cbworkspace.cpp"
    "../cbp2make/src/makefile.cpp"
    "../cbp2make/src/cbptarget.cpp"
    "../cbp2make/src/cbpunit.cpp"
    "../cbp2make/src/cbbuildcfg.cpp"
    "../cbp2make/src/cbbuildmgr.cpp"
    "../cbp2make/src/cbglobalvar.cpp"
    "../cbp2make/src/cbhelper.cpp"
    "../cbp2make/src/depsearch.cpp"
    "../cbp2make/src/platforms.cpp"
    "../cbp2make/src/toolchains.cpp"
    "../cbp2make/src/buildtools.cpp"
)

for file in "${CBP_FILES[@]}"; do
    if [ ! -f ".objs/$(basename "$file" .cpp).o" ]; then
        compile_file "$file"
    fi
done

echo ""
echo "🔸 STL 工具库 (确保包含 stringhash.cpp):"
STL_FILES=(
    "../cbp2make/lib/stlconfig.cpp"
    "../cbp2make/lib/stlconvert.cpp"
    "../cbp2make/lib/stlfutils.cpp"
    "../cbp2make/lib/stlgpm.cpp"
    "../cbp2make/lib/stlstrings.cpp"
    "../cbp2make/lib/stlvariables.cpp"
    "../cbp2make/lib/stringhash.cpp"  # 这个很重要！
)

for file in "${STL_FILES[@]}"; do
    if [ ! -f ".objs/$(basename "$file" .cpp).o" ]; then
        compile_file "$file"
    fi
done

echo ""
echo "🔸 TinyXML 核心库:"
TINYXML_FILES=(
    "../cbp2make/lib/tinyxml/tinystr.cpp"
    "../cbp2make/lib/tinyxml/tinyxml.cpp"
    "../cbp2make/lib/tinyxml/tinyxmlerror.cpp"
    "../cbp2make/lib/tinyxml/tinyxmlparser.cpp"
)

for file in "${TINYXML_FILES[@]}"; do
    if [ ! -f ".objs/$(basename "$file" .cpp).o" ]; then
        compile_file "$file"
    fi
done

# 检查编译结果
echo ""
echo "📊 编译统计:"
total_files=$(find ../cbp2make/src ../cbp2make/lib ../cbp2make/lib/tinyxml src -name "*.cpp" 2>/dev/null | grep -v xmltest | wc -l)
compiled_files=$(find .objs -name "*.o" 2>/dev/null | wc -l)
echo "  总文件数: $total_files"
echo "  已编译: $compiled_files"

if [ $compiled_files -lt $((total_files / 2)) ]; then
    echo "❌ 编译文件数量不足，可能有问题"
    exit 1
fi

# 链接
echo ""
echo "🔗 链接可执行文件..."
g++ -m64 .objs/*.o -o bin/cbp2make_ai

if [ $? -eq 0 ]; then
    echo ""
    echo "🎉 编译成功!"
    echo "📋 构建信息:"
    echo "   - 对象文件: $compiled_files 个"
    echo "   - 可执行文件: bin/cbp2make_ai"
    echo "   - 文件大小: $(du -h bin/cbp2make_ai 2>/dev/null | cut -f1 || echo "未知")"
    echo ""
    echo "💡 构建模式: 模拟模式 (使用 -DNO_CURL)"
    echo "   要使用真实AI功能，请安装 curl 并重新编译"
else
    echo "❌ 链接失败"
    echo "显示链接错误:"
    g++ -m64 .objs/*.o -o bin/cbp2make_ai 2>&1 | head -20
    exit 1
fi
